#!/bin/bash

# 部署检查脚本
# 用于验证系统部署是否成功

set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "========================================"
echo "  部署状态检查"
echo "========================================"
echo ""

ERRORS=0
WARNINGS=0

# 检查函数
check_service() {
    local service=$1
    if systemctl is-active --quiet $service; then
        echo -e "${GREEN}✓${NC} $service 服务运行中"
        return 0
    else
        echo -e "${RED}✗${NC} $service 服务未运行"
        ((ERRORS++))
        return 1
    fi
}

check_file() {
    local file=$1
    local desc=$2
    if [ -f "$file" ]; then
        echo -e "${GREEN}✓${NC} $desc: $file"
        return 0
    else
        echo -e "${RED}✗${NC} $desc 不存在: $file"
        ((ERRORS++))
        return 1
    fi
}

check_dir() {
    local dir=$1
    local desc=$2
    if [ -d "$dir" ]; then
        echo -e "${GREEN}✓${NC} $desc: $dir"
        return 0
    else
        echo -e "${RED}✗${NC} $desc 不存在: $dir"
        ((ERRORS++))
        return 1
    fi
}

check_port() {
    local port=$1
    local desc=$2
    if netstat -tuln | grep -q ":$port "; then
        echo -e "${GREEN}✓${NC} $desc 端口 $port 已监听"
        return 0
    else
        echo -e "${YELLOW}⚠${NC} $desc 端口 $port 未监听"
        ((WARNINGS++))
        return 1
    fi
}

# 1. 检查服务状态
echo "1. 检查服务状态..."
check_service "nanchang-tour" || true
check_service "nginx" || true
check_service "mysql" || true
check_service "redis-server" || check_service "redis" || true
echo ""

# 2. 检查文件结构
echo "2. 检查文件结构..."
check_dir "/opt/nanchang-tour" "部署目录"
check_dir "/opt/nanchang-tour/backend" "后端目录"
check_dir "/opt/nanchang-tour/frontend" "前端目录"
check_file "/opt/nanchang-tour/backend/.env" "环境配置文件"
check_file "/opt/nanchang-tour/backend/gunicorn_config.py" "Gunicorn配置"
check_dir "/opt/nanchang-tour/backend/staticfiles" "静态文件目录"
check_dir "/opt/nanchang-tour/frontend" "前端构建目录"
echo ""

# 3. 检查端口
echo "3. 检查端口..."
check_port "80" "HTTP"
check_port "443" "HTTPS" || true
check_port "8000" "Django后端"
check_port "3306" "MySQL"
check_port "6379" "Redis" || true
echo ""

# 4. 检查数据库连接
echo "4. 检查数据库连接..."
if [ -f "/opt/nanchang-tour/backend/.env" ]; then
    source /opt/nanchang-tour/backend/.env 2>/dev/null || true
    if mysql -u${DB_USER:-root} -p${DB_PASSWORD:-123456} -h${DB_HOST:-localhost} -e "USE ${DB_NAME:-nanchang_tour};" 2>/dev/null; then
        echo -e "${GREEN}✓${NC} 数据库连接成功"
    else
        echo -e "${RED}✗${NC} 数据库连接失败"
        ((ERRORS++))
    fi
else
    echo -e "${YELLOW}⚠${NC} 无法检查数据库（.env文件不存在）"
    ((WARNINGS++))
fi
echo ""

# 5. 检查API响应
echo "5. 检查API响应..."
if curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/api/attractions/ | grep -q "200\|301\|302"; then
    echo -e "${GREEN}✓${NC} API响应正常"
else
    echo -e "${RED}✗${NC} API响应异常"
    ((ERRORS++))
fi
echo ""

# 6. 检查前端文件
echo "6. 检查前端文件..."
if [ -f "/opt/nanchang-tour/frontend/index.html" ]; then
    echo -e "${GREEN}✓${NC} 前端index.html存在"
else
    echo -e "${RED}✗${NC} 前端index.html不存在"
    ((ERRORS++))
fi
echo ""

# 7. 检查日志目录
echo "7. 检查日志目录..."
check_dir "/var/log/nanchang-tour" "日志目录"
if [ -f "/var/log/nginx/nanchang-tour-access.log" ]; then
    echo -e "${GREEN}✓${NC} Nginx访问日志存在"
fi
if [ -f "/var/log/nginx/nanchang-tour-error.log" ]; then
    echo -e "${GREEN}✓${NC} Nginx错误日志存在"
fi
echo ""

# 总结
echo "========================================"
if [ $ERRORS -eq 0 ] && [ $WARNINGS -eq 0 ]; then
    echo -e "${GREEN}✓ 所有检查通过！${NC}"
    exit 0
elif [ $ERRORS -eq 0 ]; then
    echo -e "${YELLOW}⚠ 检查完成，有 $WARNINGS 个警告${NC}"
    exit 0
else
    echo -e "${RED}✗ 检查失败，发现 $ERRORS 个错误，$WARNINGS 个警告${NC}"
    exit 1
fi
echo "========================================"
